﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2006
' by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Search;

namespace Triax.Modules.Department
{
    /// -----------------------------------------------------------------------------
    ///<summary>
    /// The Controller class for the Department
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    public class DepartmentController : ISearchable, IPortable
    {

    #region Constructors

        public DepartmentController()
        {
        }

    #endregion

    #region Public Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// adds an object to the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="objDepartment">The DepartmentInfo object</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void AddDepartment(DepartmentInfo objDepartment)
        {
            if (objDepartment.DepartmentName.Trim() != "")
            {
                DataProvider.Instance().AddDepartment(objDepartment.ModuleId, objDepartment.DepartmentName, objDepartment.Description, objDepartment.ParentId, objDepartment.DepartmentType, objDepartment.Status, objDepartment.CreatedByUser,  objDepartment.DepartmentUserId);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// deletes an object from the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ModuleId">The Id of the module</param>
        /// <param name="ItemId">The Id of the item</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void DeleteDepartment(int ModuleId, int DepartmentId) 
        {
            DataProvider.Instance().DeleteDepartment(ModuleId,DepartmentId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// gets an object from the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="moduleId">The Id of the module</param>
        /// <param name="ItemId">The Id of the item</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public DepartmentInfo GetDepartment(int ModuleId, int DepartmentId)
        {
            return CBO.FillObject<DepartmentInfo>(DataProvider.Instance().GetDepartment(ModuleId, DepartmentId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// gets an object from the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="moduleId">The Id of the module</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public List<DepartmentInfo> GetDepartments(int ModuleId)
        {
            return CBO.FillCollection< DepartmentInfo >(DataProvider.Instance().GetDepartments(ModuleId));
        }

      

        public List<DepartmentInfo> GetDepartmentsByCond(int ModuleId, string Cond, string Sql)
        {
            return CBO.FillCollection<DepartmentInfo>(DataProvider.Instance().GetDepartmentsByCond(ModuleId, Cond, Sql));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// saves an object to the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="objDepartment">The DepartmentInfo object</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void UpdateDepartment(DepartmentInfo objDepartment)
        {
            if (objDepartment.DepartmentName.Trim() != "")
            {
                DataProvider.Instance().UpdateDepartment(objDepartment.ModuleId, objDepartment.DepartmentId, objDepartment.DepartmentName, objDepartment.Description, objDepartment.ParentId, objDepartment.DepartmentType, objDepartment.Status, objDepartment.UpdatedByUser, objDepartment.DepartmentUserId);
            }
        }

      

      
        // Customer

        public void AddCustomer(Customer objCustomer)
        {
         //  if (objCustomer.CustomerName.Trim() != "")
            {
                DataProvider.Instance().AddCustomer(objCustomer.ModuleId, 
                    
                    

objCustomer.PlanInfoID,
objCustomer.TenKhachHang,
objCustomer.DiaChi,
objCustomer.SoDienThoai,
objCustomer.Fax,
objCustomer.MST,
objCustomer.STK,
objCustomer.NguoiPhuTrach,
objCustomer.Thongtinkhac,
objCustomer.InsertDate,
objCustomer.UpdateDate,
objCustomer.CreateByUser,
objCustomer.UpdateByUser,
objCustomer.IsDelete,
objCustomer.TempInt1,
objCustomer.TempInt2,
objCustomer.TempInt3,
objCustomer.TempInt4,
objCustomer.TempStr1,
objCustomer.TempStr2,
objCustomer.TempStr3,
objCustomer.TempStr4,
objCustomer.TempDate1,
objCustomer.TempDate2

                    );
            }
        }

        public void DeleteCustomer(int ModuleId, int CustomerId)
        {
            DataProvider.Instance().DeleteCustomer(ModuleId, CustomerId);
        }

        public Customer GetCustomer(int ModuleId, int CustomerId)
        {
            return CBO.FillObject<Customer>(DataProvider.Instance().GetCustomer(ModuleId, CustomerId));
        }

        public List<Customer> GetCustomers(int ModuleId)
        {
            return CBO.FillCollection<Customer>(DataProvider.Instance().GetCustomers(ModuleId));
        }

        public void UpdateCustomer(Customer objCustomer)
        {
            // if (objCustomer.CustomerName.Trim() != "")
            {
                DataProvider.Instance().UpdateCustomer(objCustomer.ModuleId,
                    
                    
objCustomer.CustomerID,
objCustomer.PlanInfoID,
objCustomer.TenKhachHang,
objCustomer.DiaChi,
objCustomer.SoDienThoai,
objCustomer.Fax,
objCustomer.MST,
objCustomer.STK,
objCustomer.NguoiPhuTrach,
objCustomer.Thongtinkhac,
objCustomer.InsertDate,
objCustomer.UpdateDate,
objCustomer.CreateByUser,
objCustomer.UpdateByUser,
objCustomer.IsDelete,
objCustomer.TempInt1,
objCustomer.TempInt2,
objCustomer.TempInt3,
objCustomer.TempInt4,
objCustomer.TempStr1,
objCustomer.TempStr2,
objCustomer.TempStr3,
objCustomer.TempStr4,
objCustomer.TempDate1,
objCustomer.TempDate2

                    );
            }
        }


        public List<Customer> GetCustomersByCond(int ModuleId, string Cond, string Sql)
        {
            return CBO.FillCollection<Customer>(DataProvider.Instance().GetCustomersByCond(ModuleId, Cond, Sql));
        }

          
    #endregion

    #region Optional Interfaces

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetSearchItems implements the ISearchable Interface
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {
            SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();
            List<DepartmentInfo> colDepartments  = GetDepartments(ModInfo.ModuleID);

            foreach (DepartmentInfo objDepartment in colDepartments)
            {
                if(objDepartment != null)
                {
                    SearchItemInfo SearchItem = new SearchItemInfo(ModInfo.ModuleTitle, objDepartment.DepartmentName, objDepartment.CreatedByUser, objDepartment.CreatedDate, ModInfo.ModuleID, objDepartment.DepartmentId.ToString(), objDepartment.DepartmentName, "ItemId=" + objDepartment.DepartmentId.ToString());
                    SearchItemCollection.Add(SearchItem);
                }
            }

            return SearchItemCollection;
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ExportModule implements the IPortable ExportModule Interface
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ModuleID">The Id of the module to be exported</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public string ExportModule(int ModuleID)
        {
            string strXML = "";
            List<DepartmentInfo> colDepartments  = GetDepartments(ModuleID);

            if (colDepartments.Count != 0)
            {
                strXML += "<Departments>";
                foreach (DepartmentInfo objDepartment in colDepartments)
                {
                    strXML += "<Department>";
                    strXML += "<content>" + XmlUtils.XMLEncode(objDepartment.DepartmentName) + "</content>";
                    strXML += "</Department>";
                }
                strXML += "</Departments>";
            }

            return strXML;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ImportModule implements the IPortable ImportModule Interface
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ModuleID">The Id of the module to be imported</param>
        /// <param name="Content">The content to be imported</param>
        /// <param name="Version">The version of the module to be imported</param>
        /// <param name="UserId">The Id of the user performing the import</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
        {
            XmlNode xmlDepartments = Globals.GetContent(Content, "Departments");

            foreach (XmlNode xmlDepartment in xmlDepartments.SelectNodes("Department"))
            {
                DepartmentInfo objDepartment = new DepartmentInfo();

                objDepartment.ModuleId = ModuleID;
                objDepartment.DepartmentName = xmlDepartment.SelectSingleNode("content").InnerText;
                objDepartment.CreatedByUser = UserId;
                AddDepartment(objDepartment);
            }

}

    #endregion

    }
}

